#include <analysis.h>
#include <cvirte.h>		
#include <userint.h>
#include "xinhaoyuan.h"

static int panelHandle;

int main(int argc, char *argv[]) {
	if (InitCVIRTE(0, argv, 0) == 0)
		return -1; /* out of memory */
	if ((panelHandle = LoadPanel(0, "xinhaoyuan.uir", PANEL)) < 0)
		return -1;
	DisplayPanel(panelHandle);
	RunUserInterface();
	DiscardPanel(panelHandle);
	return 0;
}

int CVICALLBACK Startcall (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {
	double p,a,s,wave[20000];
	int type;

	switch (event)
	{
		case EVENT_COMMIT:
		DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_IMMEDIATE_DRAW);
		GetCtrlVal (panelHandle, PANEL_Phase, &p);
		GetCtrlVal (panelHandle, PANEL_Amplitude, &a);
		GetCtrlVal (panelHandle, PANEL_Samples, &s);
		GetCtrlVal (panelHandle, PANEL_RING, &type);
		if(s<=0) {
			s=1; //nubers of sample must >0
			SetCtrlVal (panelHandle, PANEL_LED, 1);
		}
		switch(type) {
			case 0: SineWave (s, a, 0.001, &p, wave); break;
			case 1: TriangleWave (s, a, 0.001, &p, wave);break;
			case 2: SquareWave (s, a, 0.001, &p, 50.0, wave);break;
			default:SineWave (s, a, 0.001, &p, wave); break;
		}
		PlotWaveform (panelHandle, PANEL_GRAPH, wave, s, VAL_DOUBLE, 1.0, 0.0, 0.0, 1.0,
				VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
		break;
	}
	return 0;
}

int CVICALLBACK Quitcall (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {
	switch (event) {
		case EVENT_COMMIT:
		QuitUserInterface (0);
		break;
	}
	return 0;
}

int CVICALLBACK Clearcall (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {
	switch (event) {
		case EVENT_COMMIT:
		DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_IMMEDIATE_DRAW);
		break;
	}
	return 0;
}
